# -*- coding: UTF-8 -*-
import requests
import sys
import pymongo
import re
#转换地址为经纬度并且插入数据库

reload(sys)
sys.setdefaultencoding('utf-8')

connection = pymongo.MongoClient('139.224.135.121', 27017)
db = connection.lianjia
db.authenticate("developer","qwerqwer")
ershoufang = db.ShDeal
count = ershoufang.count()
k = 6000
while k < count :
    j=k
    cursor = ershoufang.find().limit(100).skip(k)
    for i in cursor:
        try :
            searchStr = '上海市'+i['qu']+'区'+i['lu']
            r = requests.get('http://api.map.baidu.com/geocoder/v2/?address='+searchStr+'&output=json&ak=rEtXV0F4Uq6kEHDHDxP9I4hFmEYND0R0&callback=showLocation')
            print r.text;
            lng = re.compile('"lng":(.*),"lat"').findall(r.text)[0]
            lat = re.compile('"lat":(.*)},"prec').findall(r.text)[0]
            print j
            j = j + 1
            print i['_id']
            ershoufang.update({"_id":i['_id']},{"$set":{"lng":lng,"lat":lat}})
        except :
            print "出了点小问题"
    k = k + 100